package com.educate.controller;

import cn.hutool.core.map.MapUtil;
import com.educate.common.lang.Const;
import com.educate.pojo.User;
import com.educate.service.PersonalService;
import com.educate.util.OssFileUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;

/**
 * @Author: ZJQ
 * @DateTime: 2021/12/20 17:17
 * @Description: 个人中心
 */
@RestController
@RequestMapping("/personal")
@Api(tags = "个人中心相关接口")
public class PersonalController {

    @Autowired
    private OssFileUpload ossFileUpload;

    @Autowired
    private PersonalService personalService;

    @ApiOperation(value = "查询用户基本信息")
    @GetMapping("/getPersonInfo")
    public User SelectAll(Principal principal){
        String username = principal.getName();
        User user =  personalService.getPersonInfoByUsername(username);
        return user;
    }

    @ApiOperation(value = "更改用户基本信息")
    @PostMapping("/updatePersonInfo")
    public boolean updatePersonInfo(@RequestBody User user){
        System.out.println(user);
        return personalService.updatePersonal(user);
    }

    @ApiOperation(value = "上传用户头像")
    @PostMapping("/uploadAvatar")
    public String uploadAvatar(MultipartFile file){
        String fileName = ossFileUpload.ossUpload(file, Const.AVATAR_FOLDER);
        String path = Const.OSS_BASE_URL + fileName;
        personalService.updateAvatar(path);
        return path;
    }


}
